// Fluxions 3D Engine
// shaderprogram.hpp
// Copyright (C) 2000-2011 Jonathan Metzgar
// 
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#ifndef FLUXIONS_UTILS_HPP
#define FLUXIONS_UTILS_HPP

#include <string>
#include <stdio.h>

namespace Fluxions
{
	inline std::string LoadEntireTextFile(const std::string &filename)
	{
		char *buffer;
		int bufferSize;
		FILE *fin = fopen(filename.c_str(), "r");
		if (!fin)
			return NULL;
		fseek(fin, 0, SEEK_END);
		bufferSize = ftell(fin);
		buffer = new char[bufferSize+1];
		buffer[bufferSize] = 0;
		fseek(fin, 0, SEEK_SET);
		fread(buffer, bufferSize, 1, fin);
		fclose(fin);
		return std::string(buffer);
	}
	
	inline bool WriteEntireTextFile(const std::string &filename, const std::string &text)
	{
		FILE *fout = fopen(filename.c_str(), "w");
		if (!fout)
			return false;
		fwrite(text.c_str(), text.size(), 1, fout);
		fclose(fout);
		return true;
	}
	
	inline std::string StripFileExtension(const std::string &str)
	{
		int pos = str.find_last_of(".");
		if (pos == 0)
			return str;
		return str.substr(0, pos);
	}
		
}

#endif
